# Copyright (c) 2022, hzmes and contributors
# For license information, please see license.txt

import frappe
from frappe import _


def execute(filters=None):
	# get_data要传参filters
	# c = count(filters)
	message = []
	message.append("询价单单号:" + "".join(filters.get("billno")))
	# message.append(f"入库: {c}")
	return get_columns(filters), get_data(filters), message

def get_columns(filters):
	columns = [
		{
			"label": _("选中"),
			"fieldtype": "Button",
			"fieldname": "button",
			"width": 100,
		},
		{
			"label": _("Supplier"),
			"fieldtype": "Data",
			"fieldname": "supplier_name",
			"width": 150,
		},
		{
			"label": _("Material Number"),
			"fieldtype": "Data",
			"fieldname": "material_number",
			"width": 180,
		},
		{
			"label": _("Material Name"),
			"fieldtype": "Data",
			"fieldname": "material_name",
			"width": 100,
		},
		{
			"label": _("Price"),
			"fieldtype": "Data",
			"fieldname": "price",
			"width": 80,
		},
		{
			"label": _("Tax Rate"),
			"fieldtype": "Data",
			"fieldname": "tax_rate",
			"width": 80,
		},
		{
			"label": _("Tax Price"),
			"fieldtype": "Data",
			"fieldname": "tax_price",
			"width": 80,
		},
		{
			"label": _("Material Model"),
			"fieldtype": "Data",
			"fieldname": "material_model",
			"width": 300,
		},
		{
			"label": _("Selected"),
			"fieldtype": "Check",
			"fieldname": "selected",
			"width": 60,
		},
		{
			"label": _("Selected Reason"),
			"fieldtype": "Data",
			"fieldname": "selected_reason",
			"width": 200,
		},
		{
			"label": _("SRM Quotation"),
			"fieldtype": "Link",
			"options": "SRM Quotation",
			"fieldname": "parent",
			"width": 100,
		},
		{
			"label": _("Quotation Material"),
			"fieldtype": "Data",
			"fieldname": "quotation_sub_name",
			"width": 100,
		},
	]

	return columns


def get_data(filters):
	# 获取filters
	# frappe.msgprint(f'the filter is {filters}')
	billno, creation_from, creation_to = filters.get('billno'), filters.get('creation_from'), filters.get('creation_to')
	# 先配置默认conditions再通过if判断是否增加条件项
	billno = "".join(billno)
	dt = frappe.db.sql(
		f"""select i1.name as inquiry_billno, i2.name as inquiry_sub_name, q.supplier as supplier_number, q.supplier_name, q.name as quotation_sub_name, q.parent, q.price, q.tax_rate, q.tax_price,
			CONCAT('<button type=''button'' data=''', q.name ,''' onClick=''price_selected(this.getAttribute("data"))''>Button</button>') as button,
			q.selected, q.selected_reason, q.moq, q.mpq, q.lead_time,
			q.material_number, q.material_name, q.material_model
			from `tabSRM Inquiry` i1 
			join `tabSRM Inquiry Material Sub` i2 on i1.name=i2.parent 
			left join (select iq1.supplier, iq1.supplier_name, iq2.name, iq2.parent, iq2.from_inquiry_material_sub, iq2.price, iq2.tax_rate,
						iq2.selected, iq2.selected_reason, iq2.moq, iq2.mpq, iq2.lead_time,
						iq2.tax_price, iq2.material_number, iq2.material_name, iq2.material_model 
						from `tabSRM Quotation` iq1 
						join `tabSRM Quotation Material Sub` iq2 on iq1.name=iq2.parent AND iq1.docstatus = 1
					) q on i2.name=q.from_inquiry_material_sub
			WHERE i1.name = '{billno}';
		""", as_dict=True
	)

	return dt

# def count(filters):
# 	return 123